package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.mapper.DishMapper;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public  class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDto,dish,"flavors");
        this.save(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> list = flavors.stream().map(item->{
                 item.setDishId(dishId);
                 return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(list);
    }

    @Override
    public Page<DishDto> pageWithCategoryName(Integer page, Integer pageSize, String name) {
        Page<Dish> pageInit=new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();

        lqw.like(name!=null,Dish::getName,name).orderByDesc(Dish::getUpdateTime);
        this.page(pageInit,lqw);
        List<Dish> dishList = pageInit.getRecords();
        List<DishDto> dishDtos = new ArrayList<>();
        Page<DishDto> dtoPage=new Page<>();
        BeanUtils.copyProperties(pageInit,dtoPage,"records");
        BeanUtils.copyProperties(dishList,dishDtos);
        for (int i = 0; i <dishList.size() ; i++) {
            DishDto dishDto=new DishDto();
            Dish dish = dishList.get(i);
            BeanUtils.copyProperties(dish,dishDto);
            Long categoryId = dish.getCategoryId();
            String categoryName = categoryService.getById4Type(categoryId);
            dishDto.setCategoryName(categoryName);
            dishDtos.add(i,dishDto);
        }
        dtoPage.setRecords(dishDtos);
        return dtoPage;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDto,dish);
        this.updateById(dish);
        LambdaQueryWrapper<DishFlavor> lqw=new LambdaQueryWrapper<>();
        lqw.eq(dish.getId()!=null,DishFlavor::getDishId,dish.getId());
        dishFlavorService.remove(lqw);
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> collect = flavors.stream().map(item -> {
            item.setDishId(dish.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(collect);
    }

    @Override
    public void deWithFlavor(List<Long> ids) {
        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
        lqw.in(Dish::getId,ids).eq(Dish::getStatus,1);
        int count = this.count(lqw);
        if(count>0){
            throw new CustomException("菜品未停售,无法删除");
        }
        this.removeByIds(ids);
        LambdaQueryWrapper<DishFlavor> qw=new LambdaQueryWrapper<>();
        qw.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(qw);
    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
        lqw.in(Dish::getId,ids);
        List<Dish> list = this.list(lqw);
        List<Dish> collect = list.stream().map(item -> {
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());
        this.updateBatchById(collect);
    }
}
